#!/bin/sh
# filename: dcheck
# description: check system
# Toan Nguyen, June 2012

#     Copyright © 2012-2013 Toan Nguyen <toan.nguyen@monash.edu> 
#
#     This file is part of Xglom.
# 
#     This is free software: you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.
# 
#     This is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
# 
#     You should have received a copy of the GNU General Public License
#     along with this program.  If not, see <http://www.gnu.org/licenses/>.

# check if java exists
if [[ ! -z $JAVA_HOME ]]; then
	export PATH=$JAVA_HOME/bin:$PATH
fi
if [[ -z $(which java) ]]; then
	echo "Error: could not find java."
	exit 1
fi

# check if java version is equal to or greater than 1.5 
JAVA_VERSION=$(java -version 2>&1 | grep "java version" | awk '{print $3}' | tr -d \" | awk '{split($0, array, ".")} END{print array[2]}')
if [[ $JAVA_VERSION -lt 5 ]]; then
	echo "Error: java version is lower than 1.5."
	exit 1
fi

# check if $MFCOMMAND exists
if [ -z $MFCOMMAND ]
then {

    MFCOMMAND=./mfcommand
    if [[ ! -f $MFCOMMAND ]]; then
	echo "Error: could not find $MFCOMMAND"
	exit 1
    fi

    # check if $MFCOMMAND is executable
    if [[ ! -x $MFCOMMAND ]]; then
	chmod +x $MFCOMMAND
	if [[ $? -eq 1 ]]; then
		echo "Error: $MFCOMMAND is not executable. run \"sudo chmod +x $MFCOMMAND\" to fix it."
		exit 1
        fi
    fi
    
    export MFCOMMAND
}
fi

# check if $MFCOMMANDXML exists
if [ -z ${MFCOMMANDXML} ]
then {

    MFCOMMANDXML=./mfcommandxml
    if [[ ! -f ${MFCOMMANDXML} ]]; then
	echo "Error: could not find $MFCOMMANDXML"
	exit 1
    fi

    # check if $MFCOMMAND is executable
    if [[ ! -x $MFCOMMANDXML ]]; then
	chmod +x $MFCOMMANDXML
	if [[ $? -eq 1 ]]; then
		echo "Error: $MFCOMMANDXML is not executable. run \"sudo chmod +x $MFCOMMANDXML\" to fix it."
		exit 1
        fi
    fi
    
    export MFCOMMANDXML
}
fi
